#coding=utf-8

import pickle
from common.write_log import writelog
from conf.settings import BASE_DIR
import os
# import datetime
import time
import random

def write_object(obj, obj_name=None):
    try:
        print(obj)
        if obj_name is None:
            obj_name = str(random.randint(1000, 9999))

        obj_path = os.path.join(BASE_DIR, 'obj')
        date = time.strftime('%Y-%m-%d')
        filename = date+'-'+obj_name
        file = os.path.join(obj_path, filename)
        # 存为对象
        fp = open(file, 'wb')
        pickle.dump(obj, fp)
        fp.close()
        writelog('对象文件名:'+filename)
    except Exception, ex:
        print(str(ex))

def read_object(filename):
    log_path = os.path.join(BASE_DIR, 'obj')
    file = os.path.join(log_path, filename)
    # 恢复一个对象
    f = open(file, 'rb')
    data = pickle.load(f)
    print(data)
    return data


if __name__ == '__main__':
    # write_object({'s':1})
    read_object('2019-02-285392')